#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>

#define LEN 1024
#define NUM 32

int main()
{
  char cmd[LEN];
  char* myarg[NUM];
  while(1){
    printf("[zwb@VM-0-13-centos Testshell]# ");
    fgets(cmd,LEN,stdin);
    cmd[strlen(cmd)-1] = '\0';

    char* start = cmd;
    while(*start != '\0'){
      if(*start == '>'){
        *start = '\0';
        start++;
        break;
      }
      start++;
    }
    if(*start != '\0'){
      while(isspace(*start))
      {
        start++;
      }
    }

    myarg[0] = strtok(cmd," ");
    int i= 1;
    while(myarg[i] = strtok(NULL," ")){
      i++;
    }
    //printf("%s",cmd);
    pid_t id =fork();
    if(id == 0){
      //child
      execvp(myarg[0],myarg);
      exit(11);
    }

    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret > 0){
      printf("exit code: %d\n",WEXITSTATUS(status));
    }
  }
  return 0;
}
